package cn.shopifymall.processor;

import cn.shopifymall.Tomcat;
import cn.shopifymall.request.Request;
import cn.shopifymall.response.Response;
import cn.shopifymall.utils.Context;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * @Author 孤风雪影
 * @Email gitee.com/efairy520
 * @Date 2024/2/26 23:21
 * @Version 1.0
 */
public class SocketProcessor implements Runnable {

    private Socket socket;
    private Tomcat tomcat;

    public SocketProcessor(Socket socket, Tomcat tomcat) {
        this.socket = socket;
        this.tomcat = tomcat;
    }

    @Override
    public void run() {
        processSocket(socket, tomcat);
    }

    private void processSocket(Socket socket, Tomcat tomcat) {
        //处理socket连接，读数据，写数据
        try {
            InputStream inputStream = socket.getInputStream();
            //1kb，字节数组
            byte[] bytes = new byte[1024];
            inputStream.read(bytes);

            //直接打印字节数组
            //for (byte b : bytes) {
            //    System.out.print((char)b);
            //}

            //解析请求类型
            int pos = 0;
            int begin = 0, end = 0;
            for (; pos < bytes.length; pos++, end++) {
                //以空格分隔
                if (bytes[pos] == ' ') break;
            }
            //组合空格之前的字节流，转换成字符串就是请求方法
            StringBuffer method = new StringBuffer();
            for (; begin < end; begin++) {
                method.append((char) bytes[begin]);
            }
            System.out.println(method);

            //解析uri
            pos++;
            begin++;
            end++;
            for (; pos < bytes.length; pos++, end++) {
                if (bytes[pos] == ' ') break;
            }
            StringBuffer requestURL = new StringBuffer();
            for (; begin < end; begin++) {
                requestURL.append((char) bytes[begin]);
            }
            System.out.println(requestURL);

            //解析protocol
            pos++;
            begin++;
            end++;
            for (; pos < bytes.length; pos++, end++) {
                if (bytes[pos] == '\r') break;
            }
            StringBuffer protocol = new StringBuffer();
            for (; begin < end; begin++) {
                protocol.append((char) bytes[begin]);
            }
            System.out.println(protocol);
            System.out.println("------------------");

            //创建request，response
            Request request = new Request(method.toString(), requestURL, protocol.toString(), socket);
            Response response = new Response(request);

            //匹配Servlet
            //YKServlet ykServlet = new YKServlet();
            //ykServlet.service(request, response);

            String[] split = requestURL.substring(1).split("/");
            if (split.length > 1) {
                String appName = split[0];
                Context context = tomcat.getContextMap().get(appName);
                Servlet servlet = context.getByUrlPattern("/" + split[1]);
                if (servlet != null) {
                    servlet.service(request, response);
                    //发送响应
                    response.complete();
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    }
}
